/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.user;

import ca.trader.db.UserTableConstraints;
import ca.trader.regions.Country;
import ca.trader.regions.Location;
import ca.trader.regions.Region;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Transient;

/**
 *
 * @author Ahsan
 */
@Entity
@NamedQueries(
{
	 @NamedQuery(name = UserEntityConsts.NAMED_QUERY_FIND_USER_BY_EMAIL,
	 query = "SELECT u FROM User u WHERE u.email = :" + UserEntityConsts.NAMED_QUERY_FIND_USER_BY_EMAIL_PARAM_EMAIL),
})
public class User implements Serializable
{

	 @Column(length = UserTableConstraints.CONSTRAINT_ID_MAX_LENGTH, nullable = false)
	 @Id
	 @SequenceGenerator(name = "USER_ID_SEQ", allocationSize = 1, initialValue = 1)
	 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_ID_SEQ")
	 private Integer id;
	 @Column(length = UserTableConstraints.CONSTRAINT_LAST_NAME_MAX_LENGTH, nullable = false)
	 private String lastName;
	 @Column(length = UserTableConstraints.CONSTRAINT_FIRST_NAME_MAX_LENGTH, nullable = false)
	 private String firstName;
	 @Column(length = UserTableConstraints.CONSTRAINT_EMAIL_MAX_LENGTH, nullable = false, unique = true)
	 private String email;
	 @Embedded
	 private Location location;
	 @Column(length = 1, nullable = false)
	 private short activated = 0;
	 @Column(length = UserTableConstraints.CONSTRAINT_PASSWORD_MAX_LENGTH, nullable = false)
	 private String cryptedPassword;

	 public String getCryptedPassword()
	 {
		  return cryptedPassword;
	 }

	 public void setCryptedPassword(String cryptedPassword)
	 {
		  this.cryptedPassword = cryptedPassword;
	 }

	 public boolean isActivated()
	 {
		  return activated != 0;
	 }

	 public void setActivated(boolean activated)
	 {
		  this.activated = activated ? (short) 1 : (short) 0;
	 }

	 public Integer getId()
	 {
		  return id;
	 }

	 public String getLastName()
	 {
		  return lastName;
	 }

	 public void setLastName(String lastName)
	 {
		  this.lastName = lastName;
	 }

	 public String getFirstName()
	 {
		  return firstName;
	 }

	 public void setFirstName(String firstName)
	 {
		  this.firstName = firstName;
	 }

	 public String getEmail()
	 {
		  return email;
	 }

	 public void setEmail(String email)
	 {
		  this.email = email;
	 }

	 public Location getLocation()
	 {
		  return location;
	 }

	 public void setLocation(Location location)
	 {
		  this.location = location;
	 }

	 @Transient
	 public Country getCountry()
	 {
		  return location.getCountry();
	 }

	 @Transient
	 public Region getRegion()
	 {
		  return location.getRegion();
	 }

	 @Transient
	 public String getCity()
	 {
		  return location.getCity();
	 }

}
